package cn.fl.project.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;

import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.DateUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.customer.vo.CstBscInfoVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.project.condition.PrjCstInvestRptQueryCondition;
import cn.fl.project.service.IPrjBscInfoService;
import cn.fl.project.service.IPrjCstInvestRptService;
import cn.fl.project.vo.PrjCstInvestRptVO;
import cn.fl.system.constant.SysLogConstant;

/**
 * @Description: 客户调查报告表 控制层
 */
@Controller
@RequestMapping("/project/prjcstinvestrpt")
public class PrjCstInvestRptController extends BaseController {
	private static final Logger logger = LoggerFactory.getLogger(PrjCstInvestRptController.class);

	/**
	 * PrjCstInvestRpt 服务层BEAN
	 */
	@Autowired
	private IPrjCstInvestRptService prjCstInvestRptService;
	/**
	 * prjBscInfoService 服务层BEAN
	 */
	@Autowired
	private IPrjBscInfoService prjBscInfoService;

	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toAddPage.do", method = { RequestMethod.GET})
	public ModelAndView toAddPage(HttpServletRequest request, HttpServletResponse response) {
		String prjId = request.getParameter("prjId");
		String detailFlag = request.getParameter("detailFlag");
		String cstRlCd  = request.getParameter("cstRlCd");
		Long cstId =null;
		if(StringUtils.isNotBlank(prjId)){
			CstBscInfoVO cst = 	this.prjBscInfoService.getCstTypByPrjId(Long.valueOf(prjId));
			if(null!=cst){
				cstId = cst.getId();
			}
		}
		PrjCstInvestRptVO rpt  = null;
		
		List<KeyValueVO> keyList = this.prjBscInfoService.getCstRptTypList();//报告类型
		if(StringUtils.isNotBlank(prjId)&&null!=cstId){
			rpt = this.prjCstInvestRptService.getPrjCstInvestRptByPrjIdAndCstId(Long.valueOf(prjId),cstId,cstRlCd);//通过项目和客户id查询客户调查报告
		}
		ModelAndView view = new ModelAndView("project/prjcstinvestrpt/projectCstInvestRptAdd");
		view.addObject("prjId", prjId);
		view.addObject("cstId", cstId);
		view.addObject("prjCstInvestRpt", rpt);
		view.addObject("keyList",keyList);
		view.addObject("detailFlag",detailFlag);
		view.addObject("cstRlCd", cstRlCd);
		BaseLogUtils.info(logger, "toListPage", "跳转到列表页面.", CurrentThreadContext.getCurrentUserName());
		return view;
	}

	/**
	 * 到修改页面
	 */
	@RequestMapping(value = "/toModifyPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toModifyPage(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		ModelAndView view = new ModelAndView("project/prjCstInvestRpt/modify");
		view.addObject("id", id);
		BaseLogUtils.info(logger, "toModifyPage", "跳转到修改页面.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		return view;
	}

	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String searchName = request.getParameter("demo.name");
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(currentPage)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}

		PrjCstInvestRptQueryCondition qc = new PrjCstInvestRptQueryCondition();
		if (StringUtils.isNotEmpty(searchName)) {
		}
		page.setCondition(qc);

		//TODO SOMETHING
		Pager pager = this.prjCstInvestRptService.searchListPage(page);
		List<PrjCstInvestRptVO> list = (List<PrjCstInvestRptVO>) pager.getResultList();
		ListPageVO<PrjCstInvestRptVO> listPage = new ListPageVO<PrjCstInvestRptVO>(list, pager.getTotalCount());
		BaseLogUtils.info(logger, "initList", "查询数据完成. DATA:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
		return listPage;
	}

	/**
	 * 删除数据
	 */
	@RequestMapping(value = "/removeById.do", method = {RequestMethod.POST })
	@ResponseBody
	public DataResultVO removeById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_DELETE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			return result;
		}
		try {
			this.prjCstInvestRptService.removeById(Long.valueOf(id));
			BaseLogUtils.info(logger, "removeById", "删除数据.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "removeById", "删除数据失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "removeById", "删除数据失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}

		return result;
	}

	/**
	 * 得到一条记录
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getById.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.error(logger, "getById", "获取数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		
		try {
			PrjCstInvestRptVO vo = this.prjCstInvestRptService.selectById(Long.valueOf(id));
			if(null == vo){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_QUERY_DATA_IS_EMPTY);
				BaseLogUtils.info(logger, "getById", "获取数据,数据不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			}
			result.setData(vo);
			BaseLogUtils.info(logger, "getById", "获取数据.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		}catch(BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "getById", "获取失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch(Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "getById", "获取失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}

	/**
	 * 新增
	 */
	@RequestMapping(value = "/add.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object add(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String rptId = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);
		PrjCstInvestRptVO entity = null;
		Long id = null;
		try {
			PrjCstInvestRptVO prjCstInvestRptVO = getPrjCstInvestRptParam(request);
			if(StringUtils.isNotBlank(rptId)){
				//修改客户调查报告
				prjCstInvestRptVO.setId(Long.valueOf(rptId));
				this.prjCstInvestRptService.modify(prjCstInvestRptVO);
				result.setSuccess(BaseConstant.IS_YESNO_YES);
				result.setInfo(SysLogConstant.LOG_CMM_UPDATE_SUCC);
				id=Long.valueOf(rptId);
			}else{
				//获取页面参数
				prjCstInvestRptVO.setCrtTm(new Date());
				prjCstInvestRptVO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
				prjCstInvestRptVO.setIsDel(BaseConstant.IS_YESNO_NO);
				 id = this.prjCstInvestRptService.add(prjCstInvestRptVO);
			}
			result.setData(id);
			BaseLogUtils.info(logger, "add", "新增数据.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "add", "新增数据失败.DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "add", "新增数据失败.DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}

		return result;
	}

	private PrjCstInvestRptVO getPrjCstInvestRptParam(HttpServletRequest request) {
		PrjCstInvestRptVO rpt = new PrjCstInvestRptVO();
		String prjId = request.getParameter("prjId");
		String cstId = request.getParameter("cstId");
		String sugg = request.getParameter("sugg");
		String cstRptTypCd = request.getParameter("cstRptTypCd");
		String investor = request.getParameter("investor");
		String rptDt = request.getParameter("rptDt");
		String cstRlCd = request.getParameter("cstRlCd");//客户关系
		if(StringUtils.isNotBlank(prjId)){
			rpt.setPrjId(Long.valueOf(prjId));
		}
		rpt.setCstRlCd(cstRlCd);
		if(StringUtils.isNotBlank(cstId)){
			rpt.setCstId(Long.valueOf(cstId));
		}
			rpt.setSugg(sugg);
			rpt.setCstRptTypCd(cstRptTypCd);
			rpt.setInvestor(investor);
		if(StringUtils.isNotBlank(rptDt)){
			rpt.setRptDt(DateUtils.formatStringToDate(rptDt, DateUtils.YYYY_MM_DD));
		}
		rpt.setMdfTm(new Date());
		rpt.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
		return rpt;
	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "/modify.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object modify(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_UPDATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.info(logger, "modify", "修改数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		PrjCstInvestRptVO entity = null;
		try {
			entity = new PrjCstInvestRptVO();
			entity.setId(Long.valueOf(id));
			//entity.setName(name);
			//TODO ADD PARAM
			boolean success = this.prjCstInvestRptService.modify(entity);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
				BaseLogUtils.info(logger, "modify", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
			}
			BaseLogUtils.info(logger, "modify", "修改成功.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "modify", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "modify", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}
	
	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toGuarAddPage.do", method = { RequestMethod.GET})
	public ModelAndView toGuarAddPage(HttpServletRequest request, HttpServletResponse response) {
		String prjId = request.getParameter("prjId");
		String detailFlag = request.getParameter("detailFlag");
		String cstRlCd  = request.getParameter("cstRlCd");
		String cstId = request.getParameter("cstId");
		PrjCstInvestRptVO rpt  = null;
		List<KeyValueVO> keyList = this.prjBscInfoService.getCstRptTypList();//报告类型
		if(StringUtils.isNotBlank(prjId)&&null!=cstId){
			rpt = this.prjCstInvestRptService.getPrjCstInvestRptByPrjIdAndCstId(Long.valueOf(prjId),Long.valueOf(cstId),cstRlCd);//通过项目和客户id查询客户调查报告
		}
		ModelAndView view = new ModelAndView("project/prjcstinvestrpt/projectGuarInvestRptAdd");
		view.addObject("prjId", prjId);
		view.addObject("cstId", cstId);
		view.addObject("prjCstInvestRpt", rpt);
		view.addObject("keyList",keyList);
		view.addObject("detailFlag",detailFlag);
		view.addObject("cstRlCd", cstRlCd);
		BaseLogUtils.info(logger, "toListPage", "跳转到列表页面.", CurrentThreadContext.getCurrentUserName());
		return view;
	}

}

